package com.maizhi.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;


import com.maizhi.dao.BaseDaoI;
import com.maizhi.httpmodel.EasyuiDataGridJson;
import com.maizhi.httpmodel.Json;
import com.maizhi.httpmodel.LigerGrid;
import com.maizhi.httpmodel.MenuI;
import com.maizhi.httpmodel.SessionInfo;
import com.maizhi.httpmodel.UserI;
import com.maizhi.model.Menu;
import com.maizhi.model.User;
import com.maizhi.model.UserRole;
import com.maizhi.service.BaseServiceI;
import com.maizhi.service.MenuServiceI;
import com.maizhi.service.UserServiceI;
import com.maizhi.util.ResourceUtil;

@Controller
@RequestMapping("/userController")
public class userController extends BaseController {

private Logger log = Logger.getLogger(userController.class);
	
	BaseServiceI<User> baseService;
	UserServiceI userService;
	 
	BaseDaoI<User> baseDao;

	public BaseDaoI<User> getBaseDao() {
		return baseDao;
	}

	@Autowired
	public void setBaseDao(BaseDaoI<User> baseDao) {
		this.baseDao = baseDao;
	}
	
	public UserServiceI getUserService() {
		return userService;
	}

	@Autowired
	public void setUserService(UserServiceI userService) {
		this.userService = userService;
	}

	public BaseServiceI<User> getBaseService() {
		return baseService;
	}

	@Autowired
	public void setBaseService(BaseServiceI<User> baseService) {
		this.baseService = baseService;
	}
	 
	//用户管理
	@RequestMapping(params = "user")
	public String userManger() {
		return "/xtgl/user_manger";
	}
	
	/**
	 * 获取用户datagrid
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(params = "userGrid")
	@ResponseBody
	public EasyuiDataGridJson userGrid(String deptId) {
		 List<User> list=null;
		 String hql="FROM User t where t.status=1";
		 if(!(deptId==null || deptId.equals(""))){
			 hql=hql+" and t.dept.id='"+deptId+"'";
		 }
		 list = baseDao.find(hql);
		 
		 EasyuiDataGridJson ed = new EasyuiDataGridJson();
		 List<UserI> mml = new ArrayList<UserI>();
		 for(User m :  list)
		 {
			 UserI mm = new UserI();
			 BeanUtils.copyProperties(m,  mm);
			 if(m.getDept()!=null){
				 mm.setDeptId(m.getDept().getId());
				 mm.setDeptName(m.getDept().getText());
			 }
			 Set<UserRole> userRoles = m.getRoles();
			 if(userRoles!=null && userRoles.size()>0){
				 	boolean b = false;
					String roleId = "";
					String roleText = "";
				 for(UserRole ur : userRoles){
					 if(!b){
						 b=true;
					 }else{
						 roleId+=",";
						 roleText+=",";
					 }
					 roleId+=ur.getRole().getId();
					 roleText+=ur.getRole().getText();
				 }
				 mm.setRoleId(roleId);
				 mm.setRoleName(roleText);
			 }
			 mml.add(mm);
		 }
		 ed.setRows(mml);
		 ed.setTotal((long)list.size());
		 return ed;
	}
	
	
	@RequestMapping(params = "add")
	@ResponseBody
	public UserI add(UserI ui) {
		return userService.add(ui);
	}
	
	@RequestMapping(params = "edit")
	@ResponseBody
	public UserI edit(UserI ui) {
		return userService.edit(ui);
	}
	
	/**
	 * 删除用户
	 * 
	 * @param ids
	 * @return
	 */
	@RequestMapping(params = "del")
	@ResponseBody
	public Json del(String ids) {
		Json j = new Json();
		userService.del(ids);
		j.setSuccess(true);
		return j;
	}
	 
}
